package pers.qianyu.month_202011.date_20201125;

/**
 * [leetcode 每日一题]
 * 1370. 上升下降字符串
 * https://leetcode-cn.com/problems/increasing-decreasing-string/
 *
 * @author mizzle rain
 * @date 2020-11-25 16:36
 */
public class SortString {
    public String sortString(String s) {
        int[] map = new int[26];
        for (int i = 0; i < s.length(); i++) {
            map[s.charAt(i) - 'a']++;
        }
        StringBuilder builder = new StringBuilder();
        while (builder.length() < s.length()) {
            for (int i = 0; i < 26; i++) {
                if (map[i] > 0) {
                    builder.append((char) ('a' + i));
                    map[i]--;
                }
            }
            for (int i = 25; i >= 0; i--) {
                if (map[i] > 0) {
                    builder.append((char) ('a' + i));
                    map[i]--;
                }
            }
        }
        return builder.toString();
    }
}
